//
//  DrugClassController.m
//  pocketmedicine
//
//  Created by student on 16/5/9.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "DrugClassController.h"
#import "DrugShowController.h"
#import <SVProgressHUD.h>
@interface DrugClassController()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableViewList;

@property (strong,nonatomic)NSArray *arr;
@property (strong,nonatomic)NSMutableArray *listArr;
@property (strong,nonatomic)NSMutableArray *drugArr;



@end
@implementation DrugClassController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableViewList.delegate = self;
    self.tableViewList.dataSource = self;
     NSString *httpUrl = @"http://www.tngou.net/api/drug/list";
    NSString *httpArg = [NSString stringWithFormat:@"id=%@",self.drugId];
    [self request:httpUrl withHttpArg:httpArg];
  
    

}
- (IBAction)cancel:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)request: (NSString*)httpUrl withHttpArg: (NSString*)httpArg
{   [SVProgressHUD showWithStatus:@"正在加载"];
    // 网址字符串
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@?%@", httpUrl, httpArg];
    // 网址
    NSURL *url = [NSURL URLWithString: urlStr];
    // 请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    // GET请求
    [request setHTTPMethod: @"POST"];
    // 设置请求头
    [request addValue: @"639a5ad1fd83cbabd5b467f9201a5c8c" forHTTPHeaderField: @"apikey"];
    
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   //                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   //                                   NSLog(@"HttpResponseBody %@",responseString);
                                   
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                                   self.arr = [dict objectForKey:@"tngou"];
                                   //                                   NSLog(@"%@",self.arr[0]);
                                   //初始化成员变量
                                   _listArr = [[NSMutableArray alloc]init];
                                   self.drugArr = [[NSMutableArray alloc] init];
                                  
                                   
                                   for (int i = 0; i<self.arr.count; i++) {
                                       
                                       //按数组中的索引取出对应的字典
                                       NSDictionary *listdic = [self.arr objectAtIndex:i];
                                       self.medicine = [[MedicineModel alloc] initWithDict:listdic];
                                       
                                     
                                       NSString *drugId = [NSString stringWithFormat:@"%ld",self.medicine.medicineId];
                                       //通过字典中的key取出对应value，并且强制转化为NSString类型
                                       NSString *drugName = (NSString *)[listdic objectForKey:@"name"];
                                       
                                       //将获取的value值放到数组容器中
                                       [_listArr addObject:drugName];
                                       [self.drugArr addObject:self.medicine];
                                       
                                       NSLog(@"药名--》%@", drugName );
                                   }
                                   
                                   
                                   [self.tableViewList reloadData];
                                   
                            
                                   
                               }
                               
                               if (self.tableViewList!=nil) {
                                   self.tableViewList.hidden = NO;
                                   [SVProgressHUD dismiss];
                                   
                               }else {
                                   self.tableViewList.hidden = YES;
                                   [SVProgressHUD showWithStatus:@"加载错误"];
                               }

                           }];
    

    

    
}
                                   
                                   

                               


                               
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return _listArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath
{
    
    DrugShowController *dc = [self.storyboard instantiateViewControllerWithIdentifier:@"ShowVC1"];
    [SVProgressHUD showWithStatus:@"正在加载"];
    [self presentViewController:dc animated:YES completion:nil];
    dc.medicine = self.drugArr[indexPath.row];
    if (dc.medicine!=nil) {
        [SVProgressHUD dismiss];}
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [_listArr objectAtIndex:indexPath.row];
    
    return cell;
}


@end
